var userDao = require('../../dao/user.js');
var md5 = require('../../utils/md5.js').MD5;
module.exports = function(app) {
	app.post('/api/user/login', function(req, res) {
		var name = req.body.name;
		var pwd = req.body.password;
		userDao.login({
			userName: name,
			userPwd: pwd
		}, function(isSuccess, err, users) {
			if (isSuccess && users.length == 1) {
				res.json({
					status: isSuccess,
					err: '',
					user: users[0]
				});
			} else {
				res.json({
					status: false,
					err: err,
					user: null
				});
			}
		});
	});

	app.post('/api/user/register', function(req, res) {
		userDao.checkUserName(req.body.userName, function(isAble, message) {
			if (!isAble) {
				res.json({
					status: false,
					message: message
				});
			} else {
				userDao.register(req.body, function(isSuccess, err, user) {
					if (isSuccess) {
						res.json({
							status: true,
							message: '注册成功!'
						});
					} else {
						res.json({
							status: false,
							message: '数据库错误',
							err: err
						});
					}
				});
			}
		});
	});

	app.post('/api/user/check', function(req, res) {
		var name = req.body.name;
		userDao.checkUserName(name, function(isAble, message) {
			res.json({
				status: isAble,
				message: message
			});
		});
	});

	app.post('/api/user/userinfo', function(req, res) {
		var id = req.body._id;
		userDao.findUserById(id, function(isSuccess, user) {
			res.json({
				status: user != null,
				user: user
			});
		});
	});

	app.post('/api/user/UpUserInfo', function(req, res) {

		console.log(req.body);
		var id = req.body.id;
		var pwd = req.body.password;
		var gender = req.body.gender;
		var habits = req.body.habits.split(',');
		var introduction = req.body.introduction;
		var birthday = req.body.birthday.split('-');
		console.log(req.body);
		userDao.findUserById(id, function(isSuccess, user) {
			if (user == null) {
				res.json({
					status: false,
					message: '找不到该用户',
					user: null
				});
			} else {
				if (user.password != md5(pwd)) {
					res.json({
						status: false,
						message: '密码不正确',
						user: null
					});
				} else {
					user.gender = gender;
					user.habits = habits;
					user.introduction = introduction;
					user.birthday = Date.parse(birthday);
					user.save(function(err, u) {
						if (err) {
							res.json({
								status: false,
								message: '修改失败,稍后再试!',
								user: null
							});
						} else {
							res.json({
								status: true,
								message: '修改成功',
								user: u
							});
						}
					});
				}
			}
		});
	});

	app.post('/api/user/upIcon', function(req, res) {
		var id = req.body.id;
		var icon = req.body.iconUrl;
		userDao.findUserById(id, function(isSuccess, user) {
			if (isSuccess) {
				if (user) {
					user.icon = icon;
					user.save(function(err, u) {
						if (err) {
							res.json({
								status: false,
								message: '修改失败,稍后再试!',
								user: user
							})
						} else {
							res.json({
								status: true,
								message: '修改成功!',
								user: u
							})
						}
					});
				} else {
					res.json({
						status: false,
						message: '找不到该用户!',
						user: null
					})
				}
			} else {
				res.json({
					status: false,
					message: '找不到该用户!',
					user: null
				})
			}
		});
	});
};